<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Created by PhpStorm.
 * User: sino
 * Date: 2016/10/15
 * Time: 20:57
 */
class Httpkit_class
{
    public function kinkoo()
    {
        echo 'kinkoo';
    }

    public function get_http($url)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 文件流获取数据返回，输出可呈现
        return $output = curl_exec($ch);
    }

    public function get_https_ex($url, $en_Cookie = true, $cookie = '', $timeout = 30, $cookie_jar = '')
    {
        $ch = curl_init();
        if (empty($cookie_jar)) {
            $cookie_jar = dirname(__FILE__) . "/pic.cookie";
//            $cookie_jar = wxReadSession('cookie_jar'); //cookie存储文件，文件名在wxinit函数时变更
        }
        if ($en_Cookie) {//启用cookie
            if (empty($cookie)) {//判断cookie是否为空
                if (file_exists($cookie_jar)) {
                    $cookie = $this->read_cookis_ex($cookie_jar);
                } else {
                    $cookie = '';
                }
            }
        }
        $headers = array(
            'Accept: */*',
            'Accept-Language: zh-cn',
            'Content-Type: application/x-www-form-urlencoded',
            'Cookie: ' . $cookie
        );
        //个别服务器需要浏览器版本
        //curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.101 Safari/537.36");
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_HEADER, false);//不返回header部分
        //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_REFERER, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//返回字符串，而非直接输出
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);//存储cookies
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
        //curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);//等待时间
        $result = curl_exec($ch);
        //超时未启用
        /*        $curlCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                while ($curlCode <> 200) {
                    $curlCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                }*/
        curl_close($ch);
        return $result;
    }


    public function post_https($url, $post_data = array(), $header = false, $en_Cookie = true, $cookie = '', $cookie_jar = '')
    {
        $ch = curl_init();
        if (empty($cookie_jar)) {
            $cookie_jar = dirname(__FILE__) . "/pic.cookie";
            //$cookie_jar = wxReadSession('cookie_jar'); //cookie存储文件，文件名在wxinit函数时变更
        }
        if ($en_Cookie) {//启用cookie
            if (empty($cookie)) {//判断cookie是否为空
                if (file_exists($cookie_jar)) {
                    $cookie = $this->read_cookis_ex($cookie_jar);
                } else {
                    $cookie = '';
                }
            }
        }
        $headers = array(
            'Accept: */*',
            'Accept-Language: zh-cn',
            'Content-Type: application/x-www-form-urlencoded',
            'Cookie: ' . $cookie
        );
        //$data = json_encode($post_data);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; 125LA; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)");
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_HEADER, $header);//返回header部分
        //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        //curl_setopt($ch, CURLOPT_REFERER, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//返回字符串，而非直接输出
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);//存储cookies
        $result = curl_exec($ch);
//        $curlCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        $headerStr = substr($result, 0, $headerSize);

        /*        while ($curlCode <> 200) {
                    $curlCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
                }*/
        curl_close($ch);
        if ($header) {
            return $headerStr;
        }
        return $result;
    }

    public function read_cookies($file_path = '')
    {
        if (empty($file_path)) {
            $file_path = dirname(__FILE__) . "/pic.cookie";
        }
        //$file_path = $this->cookie_jar;
        return file_get_contents($file_path);
    }//普通cookie的读取

    public function read_cookis_ex($file_path = '')//读取cookie信息，已弃用
    {
        if (empty($file_path)) {
            $file_path = dirname(__FILE__) . "/pic.cookie";
        }
        $cookie = '';
        $myfile = fopen($file_path, 'r');
        while (!feof($myfile)) {
            $txt = fgets($myfile);
            if (strpos($txt, '214.194')) {
                $txt = str_replace(array("\r\n", "\r", "\n"), '', $txt);
                $textlist = (explode('	', $txt));
                $cookie .= $textlist[5] . '=' . $textlist[6] . '; ';
            }
        }
        fclose($myfile);
        return $cookie;
    }

    public function read_cookis_exa($file_path = '')//正在使用的
    {
        if (empty($file_path)) {
            $file_path = dirname(__FILE__) . "/pic.cookie";
        }
        $cookie = '{';
        $Cookie_file = fopen($file_path, 'r');
        while (!feof($Cookie_file)) {
            $txt = fgets($Cookie_file);
//            $key = $this->config->item('tableau')['TABLEAU_KEYWORD'];
            $key = '214.194';
            if (strpos($txt, $key)) {
                $txt = str_replace(array("\r\n", "\r", "\n"), '', $txt);
                $textlist = (explode('	', $txt));
                $cookie .= '"' . $textlist[5] . '":"' . $textlist[6] . '", ';
            }
        }
        fclose($Cookie_file);
        $cookie = str_replace('-', '_', $cookie);
        return $cookie . '}';
    }
}